package com.abigdreamer.im.client.module.talk;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import com.abigdreamer.im.client.module.main.SnapTipTabbedPane;
import com.abigdreamer.im.client.module.network.socket.ConnectSession;
import com.abigdreamer.im.client.module.talk.cinema.CinemaIPPort;
import com.abigdreamer.im.client.module.talk.cinema.PlayerFrame;
import com.abigdreamer.im.client.module.talk.cinema.WaitingCinemaJPanel;
import com.abigdreamer.im.client.module.talk.face.PicsJWindow;
import com.abigdreamer.im.client.module.talk.file.SendGetFileNode;
import com.abigdreamer.im.client.module.talk.file.SendGetFilePanle;
import com.abigdreamer.im.client.module.vo.Cache;
import com.abigdreamer.im.client.module.vo.MsgUser;
import com.abigdreamer.im.client.resourse.ResourcesManagement;
import com.abigdreamer.im.client.ui.BaseFrame;
import com.abigdreamer.im.client.util.EIMLogger;
import com.abigdreamer.im.client.util.FontAttrib;
import com.abigdreamer.im.client.util.InetAddressTool;
import com.abigdreamer.im.message.CinemaRequestMessage;
import com.abigdreamer.im.message.FileRequestMessage;
import com.abigdreamer.im.message.ShockFrameMessage;
import com.abigdreamer.im.message.TalkMessage;
import com.abigdreamer.net.p2p.FileProcessListener;
import com.abigdreamer.net.p2p.FileSender;
import com.abigdreamer.swing.EmptyComponent;
import com.abigdreamer.swing.app.SkinableWindow;
import com.abigdreamer.swing.component.JCButton;
import com.abigdreamer.swing.component.JCLabel;
import com.abigdreamer.swing.component.JCTextPane;
import com.abigdreamer.swing.component.JCTextPane.ChatPic;
import com.abigdreamer.swing.component.JCTextPane.FontAndText;

import craky.componentc.JCComboBox;
import craky.componentc.JCScrollPane;
import craky.layout.LineLayout;

public class TalkWindow extends SkinableWindow implements Runnable {

	private static final long serialVersionUID = -4880200042042675873L;

	public MsgUser friend; // 聊天的对方的QQ
	public static MsgUser owerUser;

	private boolean isFileSndOrGeting = false;
	public static boolean isViedoing = false;
	private CinemaIPPort cinemaIPPortBean;

	/** Creates new form QQTalkFrame */
	public TalkWindow(MsgUser friend) {
		this.friend = friend;

		setSize(540, 510);

		setTitle("与 " + friend.getUserName() + "  交谈中...   " + friend.getPersonWord());

		setIconImage(ResourcesManagement.getImage("icon/message.gif"));

		initComponents();
	}

	private void initComponents() {

		String headNum = (String) friend.getAttribute("headImage");
		setIconImage(ResourcesManagement.getImage("head/" + headNum + ".gif", 43, 43));

		userInfoFontAttrib.setSize(14);
		userInfoFontAttrib.setColor(new Color(0, 0, 255));
		userInfoFontAttrib.setName("宋体");

		getContentPane().add(createToolTab(), BorderLayout.NORTH);
		getContentPane().add(createCenterPane(), BorderLayout.CENTER);
		getContentPane().add(createEastTabPane(), BorderLayout.EAST);
	}

	private SnapTipTabbedPane createEastTabPane() {

		eastTabPane = new SnapTipTabbedPane();
		eastTabPane.addTab("个人秀秀", createXiuXiuPane());
		eastTabPane.addTab("聊天记录", new JTextPane());
		eastTabPane.setPreferredSize(new Dimension(144, -1));

		return eastTabPane;
	}

	/**
	 * create the center pane, it contain talked message pane, talk tool bar,
	 * message input pane and button pane
	 * 
	 * @author Darkness
	 * @date 2012-1-11 下午02:21:49
	 * @version V1.0
	 * @return
	 */
	private EmptyComponent createCenterPane() {

		EmptyComponent centerPane = new EmptyComponent();

		centerPane.setLayout(new LineLayout(0, 0, 0, 0, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));

		showMsgPane = new JCTextPane();
		showMsgPane.setPreferredSize(new Dimension(-1, 287));
		showMsgPane.setEditable(false);
		doc = showMsgPane.getStyledDocument();
		centerPane.add(new JCScrollPane(showMsgPane), LineLayout.START_FILL);

		centerPane.add(createTalkToolBar(), LineLayout.START);

		textFontAttrib.setSize(22);
		textFontAttrib.setColor(new Color(255, 0, 0));
		textFontAttrib.setName("宋体");
		textFontAttrib.setStyle(1);
		sendedMsgPane = new JCTextPane();
		sendedMsgPane.setCharacterAttributes(textFontAttrib.getAttrSet(), true);
		sendedMsgPane.setPreferredSize(new Dimension(-1, 80));
		centerPane.add(new JCScrollPane(sendedMsgPane), LineLayout.START_FILL);

		centerPane.add(createButtonPane(), LineLayout.START_FILL);

		return centerPane;
	}

	/**
	 * create xiuxiu pane, containe two xiuxiu:friend's and owner's
	 * 
	 * @author Darkness
	 * @date 2012-1-11 下午02:01:07
	 * @version V1.0
	 * @return
	 */
	private EmptyComponent createXiuXiuPane() {

		EmptyComponent xiuxiuPane = new EmptyComponent();
		xiuxiuPane.setLayout(new LineLayout(0, 0, 0, 0, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));

		JCLabel lblFriendXiuXiu = new JCLabel();
		lblFriendXiuXiu.setIcon(ResourcesManagement.getImageIcon("xiuxiu/4.gif"));
		xiuxiuPane.add(lblFriendXiuXiu, LineLayout.START);

		JCLabel lblOwnerXiuXiu = new JCLabel();
		lblOwnerXiuXiu.setIcon(ResourcesManagement.getImageIcon("xiuxiu/5.GIF"));
		xiuxiuPane.add(lblOwnerXiuXiu, LineLayout.START);

		return xiuxiuPane;
	}

	/* 字体名称;字号大小;文字颜色 */
	private JCComboBox fontName = null, fontSize = null, fontColor = null;
	
	/**
	 * create talk button pane, contain two buttons: close, send
	 * 
	 * @author Darkness
	 * @date 2012-1-11 下午02:07:59
	 * @version V1.0
	 * @return
	 */
	private EmptyComponent createButtonPane() {

		JCButton exit_Button = new JCButton("Close");
		exit_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				exit_ButtonMouseClicked(evt);
			}
		});

		JCButton send_Button = new JCButton("Send");
		send_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				send_ButtonMouseClicked(evt);
			}
		});

		EmptyComponent btnPane = new EmptyComponent();
		btnPane.setLayout(new LineLayout(10, 20, 0, 5, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		btnPane.setPreferredSize(new Dimension(-1, 34));

		btnPane.add(fontName = new JCComboBox(new String[] { "宋体", "黑体", "Dialog", "Gulim" }), LineLayout.START);

		btnPane.add(fontSize = new JCComboBox(new String[] { "12", "14", "18", "22", "30", "40" }), LineLayout.START);

		btnPane.add(fontColor = new JCComboBox(new String[] { "黑色", "红色", "蓝色", "黄色", "绿色" }), LineLayout.START);

		btnPane.add(exit_Button, LineLayout.START);
		btnPane.add(send_Button, LineLayout.START);

		return btnPane;
	}

	private SnapTipTabbedPane createToolTab() {

		SnapTipTabbedPane tabPane = new SnapTipTabbedPane();
		tabPane.setPreferredSize(new Dimension(-1, 58));

		JCButton snedFile_Button = new JCButton();
		snedFile_Button.setIcon(ResourcesManagement.getImageIcon("icon/export_24.gif"));
		snedFile_Button.setPreferredSize(new Dimension(24, 24));
		snedFile_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				snedFile_ButtonMouseClicked(evt);
			}
		});

		JCButton voice_Button = new JCButton();
		voice_Button.setIcon(ResourcesManagement.getImageIcon("ui/microphone4.png"));
		voice_Button.setPreferredSize(new Dimension(24, 24));
		voice_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				voice_ButtonMouseClicked(evt);
			}
		});

		JCButton video_Button = new JCButton();
		video_Button.setIcon(ResourcesManagement.getImageIcon("icon/cam_24.gif"));
		video_Button.setPreferredSize(new Dimension(24, 24));
		video_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				video_ButtonMouseClicked(evt);
			}
		});

		JCButton btnMoblie = new JCButton();
		btnMoblie.setIcon(ResourcesManagement.getImageIcon("icon/mobile_24.gif"));
		btnMoblie.setPreferredSize(new Dimension(24, 24));

		EmptyComponent talkTab = new EmptyComponent();
		talkTab.setLayout(new LineLayout(10, 8, 0, 5, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		talkTab.setPreferredSize(new Dimension(-1, 25));
		talkTab.add(btnMoblie, LineLayout.START);
		talkTab.add(video_Button, LineLayout.START);
		talkTab.add(voice_Button, LineLayout.START);
		talkTab.add(snedFile_Button, LineLayout.START);

		tabPane.addTab("聊 天", talkTab);

		JCButton qqMusic_Button = new JCButton();
		qqMusic_Button.setIcon(ResourcesManagement.getImageIcon("ui/Music.png"));
		qqMusic_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				qqMusic_ButtonMouseClicked(evt);
			}
		});

		JCButton qqSpace_jButton = new JCButton();
		qqSpace_jButton.setIcon(ResourcesManagement.getImageIcon("ui/Home.png"));
		qqSpace_jButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				qqSpace_jButtonMouseClicked(evt);
			}
		});

		JCButton qqGame_jButton = new JCButton();
		qqGame_jButton.setIcon(ResourcesManagement.getImageIcon("ui/Games.png"));
		qqGame_jButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				qqGame_jButtonMouseClicked(evt);
			}
		});

		EmptyComponent funnyTab = new EmptyComponent();
		funnyTab.setLayout(new LineLayout(10, 8, 0, 5, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		funnyTab.setPreferredSize(new Dimension(-1, 25));
		funnyTab.add(qqGame_jButton, LineLayout.START);
		funnyTab.add(qqSpace_jButton, LineLayout.START);
		funnyTab.add(qqMusic_Button, LineLayout.START);

		tabPane.addTab("娱 乐", funnyTab);

		EmptyComponent appTab = new EmptyComponent();
		appTab.setLayout(new LineLayout(10, 8, 0, 5, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		appTab.setPreferredSize(new Dimension(-1, 25));

		JCButton findShare_jButton = new JCButton();
		findShare_jButton.setText("查看共享");
		appTab.add(findShare_jButton, LineLayout.START);

		JCButton remoteHlep_Button = new JCButton();
		remoteHlep_Button.setText("运程协助");
		remoteHlep_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				remoteHlep_ButtonMouseClicked(evt);
			}
		});
		appTab.add(remoteHlep_Button, LineLayout.START);

		tabPane.addTab("应 用", appTab);

		EmptyComponent toolTab = new EmptyComponent();
		toolTab.setLayout(new LineLayout(10, 8, 0, 5, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		toolTab.setPreferredSize(new Dimension(-1, 25));

		JCButton layoutSet_Button = new JCButton();
		layoutSet_Button.setText("版面设置");
		toolTab.add(layoutSet_Button, LineLayout.START);

		JCButton deleteFriend_Button = new JCButton();
		deleteFriend_Button.setText("删除好友");
		toolTab.add(deleteFriend_Button, LineLayout.START);

		JCButton intoBlacklist_Button = new JCButton();
		intoBlacklist_Button.setText("移入黑名单");
		toolTab.add(intoBlacklist_Button, LineLayout.START);

		tabPane.addTab("\u5de5  \u5177", toolTab);

		tabPane.getAccessibleContext().setAccessibleName("tabl1");

		return tabPane;
	}

	/**
	 * create talk tool bar, contain those buttons: font button: set the talk
	 * message's font info face button: choose some funny images for talk shock
	 * button: shock the friend's talk window when this button is click send
	 * image button: choose the image from OS for talk music button: share the
	 * music with friend talk message history button: look for the messages in
	 * history
	 * 
	 * @author Darkness
	 * @date 2012-1-11 下午02:14:09
	 * @version V1.0
	 * @return
	 */
	private EmptyComponent createTalkToolBar() {

		EmptyComponent talkToolbar = new EmptyComponent();

		talkToolbar.setLayout(new LineLayout(5, 0, 0, 0, 0, LineLayout.LEADING, LineLayout.LEADING, LineLayout.HORIZONTAL));

		JCButton btnFont = new JCButton();
		btnFont.setIcon(new ImageIcon(ResourcesManagement.getImage("icon/font.gif", 16, 16)));
		btnFont.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				font_jButtonMouseClicked(evt);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				picWindow.setVisible(false);
			}
		});
		btnFont.setPreferredSize(new Dimension(24, 24));
		talkToolbar.add(btnFont);

		picWindow = new PicsJWindow(this, new PicsJWindow.PicWindowEvent() {

			@Override
			public void onClick(ChatPic image) {
				sendedMsgPane.insertIcon(image); // 插入图片
			}
		});
		
		this.addComponentListener(new ComponentAdapter() {

			@Override
			public void componentResized(ComponentEvent e) {
				TalkWindow.this.picWindow.dispose();
			}

			@Override
			public void componentMoved(ComponentEvent e) {
				TalkWindow.this.picWindow.dispose();
			}

			@Override
			public void componentHidden(ComponentEvent e) {
				TalkWindow.this.picWindow.dispose();
			}

		});
		
		btnFace = new JCButton();
		btnFace.setIcon(new javax.swing.ImageIcon(ResourcesManagement.getImage("icon/smiley.gif", 16, 16)));
		btnFace.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				face_jButtonMouseClicked(evt);
			}
		});
		btnFace.setPreferredSize(new Dimension(24, 24));
		talkToolbar.add(btnFace);

		JCButton btnShock = new JCButton();
		btnShock.setIcon(new javax.swing.ImageIcon(ResourcesManagement.getImage("icon/zhengdong.jpg", 16, 16)));
		btnShock.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				shock_ButtonMouseClicked(evt);
			}
		});
		btnShock.setPreferredSize(new Dimension(24, 24));
		talkToolbar.add(btnShock);

		JCButton btnSendImage = new JCButton();
		btnSendImage.setIcon(new javax.swing.ImageIcon(ResourcesManagement.getImage("icon/import_24.gif", 16, 16)));
		btnSendImage.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				sendImage_jButtonMouseClicked(evt);
			}
		});
		btnSendImage.setPreferredSize(new Dimension(24, 24));
		talkToolbar.add(btnSendImage);

		JCLabel btnMusic = new JCLabel();
		btnMusic.setIcon(new javax.swing.ImageIcon(ResourcesManagement.getImage("icon/mymultimedia.gif", 16, 16)));

		talkToolbar.add(btnMusic);

		JCButton btnTalkRecord = new JCButton();
		btnTalkRecord.setText("聊天记录");
		btnTalkRecord.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				talkRecord_ButtonMouseClicked(evt);
			}
		});
		talkToolbar.add(btnTalkRecord);

		return talkToolbar;
	}

	private void insert(FontAttrib userAttrib, String message) {
		try { // 插入文本
			doc.insertString(doc.getLength(), userAttrib.getText() + "\r\n", userAttrib.getAttrSet());
//			doc.insertString(doc.getLength(), "  " + textAttrib.getText() + "\r\n", textAttrib.getAttrSet());
			showMsgPane.insert(message);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	// 选择文件还回文件的对象
	private File slecetFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.showOpenDialog(this);
		File file = fileChooser.getSelectedFile();
		return file;
	}

	public void setVisible(boolean b) {
		if (b) {
			BaseFrame.centerWindow(this);
			Cache cache = Cache.getInstance();
			cache.addTalkFrame(friend.getUserID(), this);
		} else {
			showMsgPane.setText("");
			Cache cache = Cache.getInstance();
			cache.removeTalkFrame(friend.getUserID());
			this.dispose();
		}
		super.setVisible(b);
	}

	private void font_jButtonMouseClicked(java.awt.event.MouseEvent evt) {
	}

	private void face_jButtonMouseClicked(java.awt.event.MouseEvent evt) {
		showPicWindow();
	}
	
	/* 表情框 */
	private PicsJWindow picWindow;
	
	JButton btnFace;
	
	private void showPicWindow() {
		Point loc = btnFace.getLocationOnScreen();/* 控件相对于屏幕的位置 */
		picWindow.setBounds(loc.x - picWindow.getPreferredSize().width / 3, loc.y - picWindow.getPreferredSize().height, picWindow.getPreferredSize().width, picWindow.getPreferredSize().height);
		picWindow.setVisible(true);
	}

	/**
	 * 
	 * @param evt
	 */
	private void shock_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		if (friend.isOnline()) {
			String srcQQ = owerUser.getUserID();
			String destQQ = this.friend.getUserID();
			ShockFrameMessage shockFrameMessage = new ShockFrameMessage(srcQQ, destQQ);
			ConnectSession.getInstance().sendTextMessage(shockFrameMessage);
			FrameEarthquakeCenter dec = new FrameEarthquakeCenter(this);
			dec.startShake();
		} else {
			javax.swing.JOptionPane.showMessageDialog(this, "您的好友没在线或者隐身！");
		}
	}

	private void sendImage_jButtonMouseClicked(java.awt.event.MouseEvent evt) {
	}

	/**
	 * 
	 * @param evt
	 */
	private void talkRecord_ButtonMouseClicked(java.awt.event.MouseEvent evt) {

	}

	private void remoteHlep_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
	}

	private void qqMusic_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
	}

	private void qqSpace_jButtonMouseClicked(java.awt.event.MouseEvent evt) {
	}

	private void qqGame_jButtonMouseClicked(java.awt.event.MouseEvent evt) {
	}

	/**
	 * 音频请求按钮
	 * 
	 * @param evt
	 */
	private void voice_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		System.out.println("音频请求按钮");

	}

	/**
	 * 传送文件的按钮的事件
	 * 
	 * @param evt
	 */
	private void snedFile_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		File file = slecetFile();
		if (file != null) {// 选中了文件
			FileRequestMessage message = sendFile(file);
			this.addFileJPanel(false, message);
		} else {
			javax.swing.JOptionPane.showMessageDialog(this, "你没有选中文件！");
		}

	}

	SendGetFileNode sendFileNode;

	public SendGetFileNode getFileNode() {
		return sendFileNode;
	}

	public void addFileJPanel(boolean isGet, FileRequestMessage message) {
		if (filePanle == null) {
			filePanle = new SendGetFilePanle();
		}
		sendFileNode = new SendGetFileNode(isGet, message);
		filePanle.addFilePanle(sendFileNode);
		filePanle.setParentComponent(eastTabPane);
		eastTabPane.add("文件发送", filePanle);
		eastTabPane.setPreferredSize(new Dimension(254, -1));

		eastTabPane.setSelectedComponent(filePanle);
		this.pack();
	}

	private FileRequestMessage sendFile(File file) {
		FileRequestMessage message = new FileRequestMessage(owerUser.getUserID(), friend.getUserID());
		message.setFileName(file.getName());
		message.setFileAbsolutePath(file.getAbsolutePath());

		int port = InetAddressTool.getAvailablePort();

		message.setIp(InetAddressTool.getLocalIP());
		message.setPort(port);

		java.io.FileInputStream fis = null;
		try {
			fis = new java.io.FileInputStream(file);
			long filelengh = fis.available();
			fis.close();

			message.setFileLength(filelengh);
			EIMLogger.INFO("选中的文件长度为：" + filelengh);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ConnectSession session = ConnectSession.getInstance();
		session.sendTextMessage(message);

		fileSender = new FileSender(port, file, new FileProcessListener() {

			@Override
			public void onProcess(int currentPackage, int totalPaclage) {
				sendFileNode.setProcess(currentPackage, totalPaclage);
			}

			@Override
			public void onExit() {
				removeFileNode();
			}
		});
		java.awt.EventQueue.invokeLater(fileSender);

		return message;
	}

	public void removeFileNode() {
		sendFileNode.refuseAction();
	}

	private FileSender fileSender;

	/**
	 * 视频请求按钮
	 * 
	 * @param evt
	 */
	private void video_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		if (!isViedoing) {
			addcinemaJPanel("send");
			CinemaRequestMessage cinemaRequestMessage = null;
			cinemaRequestMessage = new CinemaRequestMessage(owerUser.getUserID(), friend.getUserID());
			ConnectSession session = ConnectSession.getInstance();
			session.sendTextMessage(cinemaRequestMessage);
		} else {
			javax.swing.JOptionPane.showMessageDialog(this, "您已经正在和一个好友视频！");
		}

	}

	/**
	 * 关闭按钮的激发事件
	 * 
	 * @param evt
	 */
	private void exit_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		this.setVisible(false);
	}

	/**
	 * 发送按钮的激发事件
	 * 
	 * @param evt
	 */
	private void send_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		this.sendTalkMessage();
	}

	public void sendTalkMessage() {
		String msgStr = sendedMsgPane.getPicText();
		if (msgStr.length() == 0) {
			javax.swing.JOptionPane.showMessageDialog(this, "聊天信息不能为空!!..");
		} else {
			TalkMessage msg = new TalkMessage();
			msg.setDestQQ(friend.getUserID());
			msg.setSrcQQ(owerUser.getUserID());
			FontAndText myFont = getFontAttrib();
			msg.setTalkMsg(myFont.toString());
			ConnectSession connectSession = ConnectSession.getInstance();
			connectSession.sendTextMessage(msg);
			showMsg(owerUser.getUserName(), myFont.toString());
			sendedMsgPane.setText("");
		}
	}
	
	/**
	 * 获取所需要的文字设置
	 * 
	 * @return FontAttrib
	 */
	private FontAndText getFontAttrib() {
		FontAndText att = new FontAndText();
		att.setText(sendedMsgPane.getPicText());// 文本和表情信息
		att.setFontName((String) fontName.getSelectedItem());
		att.setFontSize(Integer.parseInt((String) fontSize.getSelectedItem()));
		String temp_color = (String) fontColor.getSelectedItem();
		if (temp_color.equals("黑色")) {
			att.setFontColor(new Color(0, 0, 0));
		} else if (temp_color.equals("红色")) {
			att.setFontColor(new Color(255, 0, 0));
		} else if (temp_color.equals("蓝色")) {
			att.setFontColor(new Color(0, 0, 255));
		} else if (temp_color.equals("黄色")) {
			att.setFontColor(new Color(255, 255, 0));
		} else if (temp_color.equals("绿色")) {
			att.setFontColor(new Color(0, 255, 0));
		}
		return att;
	}

	public void showMsg(String peak, String msg) {
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");// 这里的格式自己按需要写
		Date d = new Date();
		String dataString = sf.format(d);
		userInfoFontAttrib.setText(peak + "  " + dataString);
		System.out.println("textInfo:" + msg);
		textFontAttrib.setText(msg);
		
		this.insert(userInfoFontAttrib, msg);
	}

	public void addcinemaJPanel(String falg) {
		if (!isViedoing) {
			cinemaJPanel = new WaitingCinemaJPanel(falg);
			cinemaJPanel.setParent(eastTabPane);
			cinemaJPanel.setTalkFrameParent(this);
			eastTabPane.addTab("于" + friend.getUserName() + "视频", cinemaJPanel);
			eastTabPane.setSelectedComponent(cinemaJPanel);
			isViedoing = true;
			this.pack();
		} else {
			javax.swing.JOptionPane.showMessageDialog(this, "您已经正在和一个好友视频！");
		}
	}

	public void addCinemaPlayer(String flag, PlayerFrame playerPanel) {
		cinemaJPanel.addCinemaPlayer(flag, playerPanel);
	}

	public void removeSendGetFileProcessPanle() {
		if (cinemaJPanel != null) {
			eastTabPane.remove(cinemaJPanel);
			cinemaJPanel = null;
			isViedoing = false;
		}
	}

	public void run() {
	}

	public boolean isFileSndOrGeting() {
		return isFileSndOrGeting;
	}

	public void setFileSndOrGeting(boolean isFileSndOrGeting) {
		this.isFileSndOrGeting = isFileSndOrGeting;
	}

	public JTabbedPane getJTabbedPane() {
		return eastTabPane;
	}

	public WaitingCinemaJPanel getCinemaJPanel() {
		return cinemaJPanel;
	}

	public void setCinemaJPanel(WaitingCinemaJPanel cinemaJPanel) {
		this.cinemaJPanel = cinemaJPanel;
	}

	private FontAttrib userInfoFontAttrib = new FontAttrib();
	private FontAttrib textFontAttrib = new FontAttrib();
	private StyledDocument doc = null; // 非常重要插入文字样式就靠它了

	private WaitingCinemaJPanel cinemaJPanel;
	private SendGetFilePanle filePanle;

	private SnapTipTabbedPane eastTabPane;

	private JCTextPane sendedMsgPane; // 写入聊天信息的TextArea

	public JCTextPane showMsgPane; // 显示聊天信息的TextArea

	public CinemaIPPort getCinemaIPPortBean() {
		return cinemaIPPortBean;
	}

	public void setCinemaIPPortBean(CinemaIPPort cinemaIPPortBean) {
		this.cinemaIPPortBean = cinemaIPPortBean;
	}

	public static MsgUser getOwerUser() {
		return owerUser;
	}

	public static void setOwerUser(MsgUser owerUser) {
		TalkWindow.owerUser = owerUser;
	}

	@Override
	protected void onWindowClosing() {
		this.dispose();
	}

}
